using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using Husb.Data;
using Husb.DataUtil;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Caching;

using CentralOffice.Task.Data;

using TaskTable = CentralOffice.Task.Data.TaskDataSet.TasksDataTable;
using TaskRow = CentralOffice.Task.Data.TaskDataSet.TasksRow;

using ProgressTable = CentralOffice.Task.Data.TaskProgressDataSet.TaskProgressDataTable;
using ProgressRow = CentralOffice.Task.Data.TaskProgressDataSet.TaskProgressRow;

using HistoryTable = CentralOffice.Task.Data.TaskHistoryDataSet.TaskHistoryDataTable;
using HistoryRow = CentralOffice.Task.Data.TaskHistoryDataSet.TaskHistoryRow;

using AttachmentTable = CentralOffice.Task.Data.TaskAttachmentDataSet.TaskAttachmentsDataTable;




namespace CentralOffice.Task.DataAccess
{
    public class TaskAdapter : DataAccessAdapter<CentralOffice.Task.Data.TaskDataSet, TaskTable, TaskRow>
    {
        private const string UpdateStatusStoredProc = "Tasks.Tasks_UpdateTaskStatus";
        private const string JustUpdateStatusStoredProc = "Tasks.Tasks_JustUpdateTaskStatus";

        private const string SelectTaskAttendmentsByIdStoredProc = "Tasks.Tasks_SelectTaskAttendmentsById";
        private const string SelectTaskByEmployeeStoredProc = "Tasks.Tasks_SelectTaskByEmployee";
        public TaskAdapter()
        {
            this.TableName = "Tasks";

            GetAllStoredProc = "Tasks.Tasks_SelectTasksAll";
            InsertStoredProc = "Tasks.Tasks_InsertTask";
            UpdateStoredProc = "Tasks.Tasks_UpdateTask";
            DeleteStoredProc = "Tasks.Tasks_DeleteTask";

            GetByIdStoredProc     = "Tasks.Tasks_SelectTask";
            GetByNameStoredProc   = "";
            GetPagedStoredProc    = "Tasks.Tasks_SelectTasksPaged";
            GetRowCountStoredProc = "Tasks.Tasks_SelectTasksRowCount";
            FindStoredProc = "Tasks.Tasks_SelectTasksDynamic";

            LogicalDeleteStoredProc = "Tasks.Tasks_DeleteTaskLogical";
        }

        protected override void PopulateInsertParameters(Database db, DbCommand cmd)
        {
            db.AddInParameter(cmd, "Id", DbType.Guid, "Id", DataRowVersion.Current);
            db.AddInParameter(cmd, "Name", DbType.AnsiString, "Name", DataRowVersion.Current);
            db.AddInParameter(cmd, "CategoryId", DbType.Guid, "CategoryId", DataRowVersion.Current);
            db.AddInParameter(cmd, "TaskNumber", DbType.AnsiString, "TaskNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "QueryNumber", DbType.AnsiString, "QueryNumber", DataRowVersion.Current);
            db.AddInParameter(cmd, "Significant", DbType.AnsiString, "Significant", DataRowVersion.Current);
            db.AddInParameter(cmd, "ProjectID", DbType.AnsiString, "ProjectID", DataRowVersion.Current);
            db.AddInParameter(cmd, "Project", DbType.AnsiString, "Project", DataRowVersion.Current);
            db.AddInParameter(cmd, "Source", DbType.AnsiString, "Source", DataRowVersion.Current);
            db.AddInParameter(cmd, "Sponsor", DbType.AnsiString, "Sponsor", DataRowVersion.Current);
            db.AddInParameter(cmd, "Accepter", DbType.AnsiString, "Accepter", DataRowVersion.Current);
            db.AddInParameter(cmd, "CurrentAccepter", DbType.AnsiString, "CurrentAccepter", DataRowVersion.Current);
            db.AddInParameter(cmd, "ArrangedDate", DbType.DateTime, "ArrangedDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "IntendingDate", DbType.DateTime, "IntendingDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "CloseDate", DbType.DateTime, "CloseDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "Profile", DbType.AnsiString, "Profile", DataRowVersion.Current);
            db.AddInParameter(cmd, "ContentText", DbType.String, "ContentText", DataRowVersion.Current);
            db.AddInParameter(cmd, "Attachments", DbType.String, "Attachments", DataRowVersion.Current);
            db.AddInParameter(cmd, "Notice", DbType.AnsiString, "Notice", DataRowVersion.Current);
            db.AddInParameter(cmd, "Status", DbType.AnsiString, "Status", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovedDate", DbType.DateTime, "ApprovedDate", DataRowVersion.Current);
            db.AddInParameter(cmd, "ApprovedBy", DbType.AnsiString, "ApprovedBy", DataRowVersion.Current);

            db.AddInParameter(cmd, "IsDeleted", DbType.Boolean, "IsDeleted", DataRowVersion.Current);
            db.AddInParameter(cmd, "IsActive", DbType.Boolean, "IsActive", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedTime", DbType.DateTime, "CreatedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "CreatedBy", DbType.Guid, "CreatedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "ModifiedTime", DbType.DateTime, "ModifiedTime", DataRowVersion.Current);
            db.AddInParameter(cmd, "LastModifiedBy", DbType.Guid, "LastModifiedBy", DataRowVersion.Current);
            db.AddInParameter(cmd, "Version", DbType.Int32, "Version", DataRowVersion.Current);
            db.AddInParameter(cmd, "Description", DbType.String, "Description", DataRowVersion.Current);
        }

        public bool UpdateStatus(
            Guid id,
            string assignedBy,
            string allPrincipal,
            DateTime? closeDate,
            string status,
            DateTime? approvedDate,
            string approvedBy,
            DateTime modifiedTime,
            Guid lastModifiedBy,
            TaskProgressDataSet progress, TaskHistoryDataSet history)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("Id", DbType.Guid, id));
            parameters.Add(new DatabaseParameter("Accepter", DbType.AnsiString, allPrincipal));
            parameters.Add(new DatabaseParameter("CurrentAccepter", DbType.AnsiString, assignedBy));
            

            parameters.Add(new DatabaseParameter("CloseDate", DbType.DateTime, closeDate));
            parameters.Add(new DatabaseParameter("Status", DbType.AnsiString, status));
            parameters.Add(new DatabaseParameter("ApprovedDate", DbType.DateTime, approvedDate));
            parameters.Add(new DatabaseParameter("ApprovedBy", DbType.AnsiString, approvedBy));
            parameters.Add(new DatabaseParameter("ModifiedTime", DbType.DateTime, modifiedTime));
            parameters.Add(new DatabaseParameter("LastModifiedBy", DbType.Guid, lastModifiedBy));

            return this.ExecuteNonQuery<
                TaskProgressAdapter, TaskProgressDataSet, ProgressTable, ProgressRow,
                TaskHistoryAdapter, TaskHistoryDataSet, HistoryTable, HistoryRow>
            (UpdateStatusStoredProc, parameters, progress, history, true, false, null);
        }

        public bool UpdateStatus(
            Guid id,
            string status,
            DateTime modifiedTime,
            Guid lastModifiedBy,
            TaskProgressDataSet progress, TaskHistoryDataSet history)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("Id", DbType.Guid, id));
            parameters.Add(new DatabaseParameter("Status", DbType.AnsiString, status));
            parameters.Add(new DatabaseParameter("ModifiedTime", DbType.DateTime, modifiedTime));
            parameters.Add(new DatabaseParameter("LastModifiedBy", DbType.Guid, lastModifiedBy));

            return this.ExecuteNonQuery<
                TaskProgressAdapter, TaskProgressDataSet, ProgressTable, ProgressRow,
                TaskHistoryAdapter, TaskHistoryDataSet, HistoryTable, HistoryRow>
            (JustUpdateStatusStoredProc, parameters, progress, history, true, false, null);
        }

        //public override TaskDataSet.TasksDataTable GetPaged(int startRowIndex, int maximumRows, string sortExpression, string whereClause, string orderBy, bool isStoredProc, bool enableCache)
        //{
        //    whereClause = DataAccessUtil.AppendDeletedWhereClause(whereClause);
        //    return base.GetPaged(startRowIndex, maximumRows, sortExpression, whereClause, orderBy, isStoredProc, enableCache);
        //}

        //public override int GetCount(string whereClause, bool enableCache)
        //{
        //    whereClause = DataAccessUtil.AppendDeletedWhereClause(whereClause);
        //    return base.GetCount(whereClause, enableCache);
        //}

        public int GetTasksByEmployee(string employee)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("Employee", DbType.AnsiString, employee));

            object o = this.GetValue(SelectTaskByEmployeeStoredProc, parameters, true, false, null);

            if (o != null)
            {
                return (int)o;
            }
            return 0;

        }

        public AttachmentTable GetAttachmentsById(Guid id)
        {
            List<DatabaseParameter> parameters = new List<DatabaseParameter>();
            parameters.Add(new DatabaseParameter("Id", DbType.Guid, id));
            TaskAttachmentDataSet ds = Husb.DataUtil.DataAccessUtil.GetDataSet<TaskAttachmentDataSet>(SelectTaskAttendmentsByIdStoredProc, parameters, "TaskAttachments", true, false, null);

            AttachmentTable dt = ds.TaskAttachments;
            return dt;
        }

    }
}
